/**
 * 
 */
define(['wiseMen/dataModel/QueryDefTpl'],function(QueryDefTpl){
	webUtils.nameSpace("Control");
	webUtils.nameSpace("Control.constant");
	
	Control.singleText=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>'
				if(param.readOnly){
					viewStr+="<pre class='dataBox'>";
					if(field.defaultValue){
						viewStr+=field.defaultValue;
					}
					viewStr+="</pre>";
				}else{
					viewStr+='<input type="text" name="'+dataModelDef.code+"."+field.code+'" class="form-control" '
					if(field.defaultValue){
						viewStr+='value="'+field.defaultValue+'" ';
					}
					if(field.viewCheckType){
						viewStr+=' check-type="'+field.viewCheckType+'" ';
					}
					viewStr+='>';
				}
				viewStr+='</div>';
			return viewStr;
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace
		};
		this.afterAppend=function(){
			
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			return currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]').val()||"";
		};
		this.setValue=function(v){
			if(param.readOnly){
				currentCtr.find(".dataBox").html(v);
			}else{
				if(v!=null && v!=undefined){
					currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]').val(v);
				}
			}
		};
	};
	Control.multiText=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
			currentCtr=$(viewStr);
		};
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>';
			if(param.readOnly){
				viewStr+="<pre class='pre-scrollable dataBox'>";
				if(field.defaultValue){
					viewStr+=field.defaultValue;
				}
				viewStr+="</pre>";
			}else{
				viewStr+='<textarea type="text" name="'+dataModelDef.code+"."+field.code+'" class="form-control" '
				if(field.viewCheckType){
					viewStr+=' check-type="'+field.viewCheckType+'" ';
				}
				viewStr+='>';
				if(field.defaultValue){
					viewStr+=field.defaultValue;
				}
				viewStr+='</textarea>'
			}
				
				viewStr+='</div>';
			return viewStr;
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.afterAppend=function(){
			
		};
		this.getDataModelName=function(){
			return dataModelDef.name;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			return currentCtr.find('textarea[name="'+dataModelDef.code+"."+field.code+'"]').val()||"";
		};
		this.setValue=function(v){
			if(param.readOnly){
				currentCtr.find(".dataBox").html(v);
			}else{
				currentCtr.find('textarea[name="'+dataModelDef.code+"."+field.code+'"]').val(v);
			}
		};
	};
	Control.dictionary=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
			currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>'
				if(param.readOnly){
					viewStr+="<pre class='dataBox'>";
					viewStr+="</pre>";
				}else{
					viewStr+='<input type="text" name="'+dataModelDef.code+"."+field.code+'" class="form-control"';
					if(field.viewCheckType){
						viewStr+=' check-type="'+field.viewCheckType+'" ';
					}
					viewStr+='>';
				}
				viewStr+="</div>";
			return viewStr;
		}
		var iniEvent=function(){
			currentCtr.find("input").click(function(){
				var $thisInputCtr=$(this);
				var hasSelectedDatas=currentCtr.data("data");
				webUtils.dataDicSelector({
					dataDicCode:field.dataDicCode,
					hasSelectedDataItem:hasSelectedDatas,
					isSingle:field.isSingleSelect,
					onSelect:function(rows){
						var names=[];
						$.each(rows,function(i,n){
							names.push(n.name);
						});
						$thisInputCtr.val(names.join(","));
						currentCtr.data("data",rows);
					}
				});
			});
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.afterAppend=function(){
			if(!param.readOnly==true){
				iniEvent();
			}
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			var d=currentCtr.data("data");
			var codes=[];
			if(d){
				$.each(d,function(i,n){
					codes.push(n.code);
				});
			}
			return codes.join(",");
		};
		this.setValue=function(v){
			var datas=[];
			var names=[];
			if(v){
				var vs=v.split?v.split(","):[v];
				$.each(vs,function(i,n){
					if(n){
						var item=webUtils.getDataDicItemVal(field.dataDicCode,n);
						if(item){
							datas.push({
								name:item.name,
								code:item.code
							});
							names.push(item.name);
						}
					}
				})
				if(param.readOnly){
					currentCtr.find(".dataBox").html(names.join(","));
				}else{
					currentCtr.data("data",datas);
					currentCtr.find("input").val(names.join(","));
				}
				
			}
			
		};
	};
	Control.userSelector=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
			currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>'
				if(param.readOnly){
					viewStr+="<pre class='dataBox'>";
					viewStr+="</pre>";
				}else{
					viewStr+='<input type="text" name="'+dataModelDef.code+"."+field.code+'" class="form-control"';
					if(field.viewCheckType){
						viewStr+=' check-type="'+field.viewCheckType+'" ';
					}
					viewStr+='>';
				}
				viewStr+='</div>';
			return viewStr;
		}
		var iniEvent=function(){
			currentCtr.find("input").click(function(){
				var $inputCtr=$(this);
				var hasSelectedUsers=currentCtr.data("data");
				webUtils.userSelector({
					hasSelectedUsers:hasSelectedUsers,
					isSingle:field.isSingleSelect,
					onSelect:function(rows){
						var names=[];
						$.each(rows,function(i,n){
							names.push(n.userName);
						});
						$inputCtr.val(names.join(","));
						currentCtr.data("data",rows);
					}
				});
			});
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.afterAppend=function(){
			if(!param.readOnly==true){
				iniEvent();
			}
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			var d=currentCtr.data("data");
			var ids=[];
			if(d){
				$.each(d,function(i,n){
					ids.push(n.id);
				});
			}
			return ids.join(",");
		};
		this.setValue=function(v){
			var datas=[];
			var names=[];
			if(v){
				$.getJSON(ctx+"/userAction/queryByIds.do",{ids:v},function(ds){
					$.each(ds,function(i,n){
						datas.push({
							id:n.id,
							userName:n.userName
						});
						names.push(n.userName);
					});
					if(param.readOnly){
						currentCtr.find(".dataBox").html(names.join(","));
					}else{
						currentCtr.data("data",datas);
						currentCtr.find("input").val(names.join(","));
					}
				});
			}else if(field.isSetCurrentSession){
				var user=webUtils.getCurrentUser();
				if(user){
					currentCtr.data("data",[{
						id:user.id,
						userName:user.userName
					}]);
					currentCtr.find("input").val(user.userName);
				}
			}
		};
		
	};
	Control.orgSelector=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>'
				if(param.readOnly){
					viewStr+="<pre class='dataBox'>";
					viewStr+="</pre>";
				}else{
					viewStr+='<input type="text" name="'+dataModelDef.code+"."+field.code+'" class="form-control" '
					if(field.viewCheckType){
						viewStr+=' check-type="'+field.viewCheckType+'" ';
					}
					viewStr+='>';
				}
				viewStr+='</div>';
			return viewStr;
		}
		var iniEvent=function(){
			currentCtr.find("input").click(function(){
				var $inputCtr=$(this);
				var hasSelectedOrgs=currentCtr.data("data");
				webUtils.orgSelector({
					hasSelectedOrgs:hasSelectedOrgs,
					isSingle:field.isSingleSelect,
					onSelect:function(rows){
						var names=[];
						$.each(rows,function(i,n){
							names.push(n.name);
						});
						$inputCtr.val(names.join(","));
						currentCtr.data("data",rows);
					}
				});
			});
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.afterAppend=function(){
			if(!param.readOnly==true){
				iniEvent();
			}
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			var d=currentCtr.data("data");
			var ids=[];
			if(d){
				$.each(d,function(i,n){
					ids.push(n.id);
				});
			}
			return ids.join(",");
		};
		this.setValue=function(v){
			var datas=[];
			var names=[];
			if(v){
				$.getJSON(ctx+"/orgAction/queryByIds.do",{ids:v},function(ds){
					$.each(ds,function(i,n){
						datas.push({
							id:n.id,
							name:n.name
						});
						names.push(n.name);
					});
					if(param.readOnly){
						currentCtr.find(".dataBox").html(names.join(","));
					}else{
						currentCtr.data("data",datas);
						currentCtr.find("input").val(names.join(","));
					}
				});
			}else if(field.isSetCurrentSession){
				var user=webUtils.getCurrentUser();
				if(user){
					currentCtr.data("data",[{
						id:user.bureauOrg.id,
						name:user.bureauOrg.name
					}]);
					currentCtr.find("input").val(user.bureauOrg.name);
				}
			}
		};
		
	};
	Control.adminDivSelector=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>'
				if(param.readOnly){
					viewStr+="<pre class='dataBox'>";
					viewStr+="</pre>";
				}else{
					viewStr+='<input type="text" name="'+dataModelDef.code+"."+field.code+'" class="form-control" '
					if(field.viewCheckType){
						viewStr+=' check-type="'+field.viewCheckType+'" ';
					}
					viewStr+='>';
				}
				viewStr+='</div>';
			return viewStr;
		}
		var iniEvent=function(){
			currentCtr.find("input").click(function(){
				var $inputCtr=$(this);
				var hasSelectedAdminDiv=currentCtr.data("data");
				webUtils.adminDivSelector({
					hasSelectedAdminDivs:hasSelectedAdminDiv,
					isSingle:field.isSingleSelect,
					initAdminDivs:wiseMen.platform.initProperties.adminDiv,
					onSelect:function(rows){
						var names=[];
						$.each(rows,function(i,n){
							names.push(n.name);
						});
						$inputCtr.val(names.join(","));
						currentCtr.data("data",rows);
					}
				});
			});
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.afterAppend=function(){
			if(!param.readOnly==true){
				iniEvent();
			}
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			var d=currentCtr.data("data");
			var codes=[];
			if(d){
				$.each(d,function(i,n){
					codes.push(n.code);
				});
			}
			return codes.join(",");
		};
		this.setValue=function(v){
			var datas=[];
			var names=[];
			if(v){
				$.getJSON(ctx+"/adminDivAct/queryByCodes.do",{codes:v},function(ds){
					$.each(ds,function(i,n){
						datas.push({
							code:n.code,
							name:n.name
						});
						names.push(n.name);
					});
					if(param.readOnly){
						currentCtr.find(".dataBox").html(names.join(","));
					}else{
						currentCtr.data("data",datas);
						currentCtr.find("input").val(names.join(","));
					}
				});
			}
		};
	};
	
	Control.fileUpload=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var uploader;
		var listViewId=field.code+"_listView";
		var listUploadViewId=field.code+"_listUploadView";
		var uploadPickId=field.code+"_btView";
		
		var attSetId;
		
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>';
				if(param.readOnly){
					viewStr+="<pre class='dataBox fileList' id='"+listViewId+"'>";
					viewStr+="</pre>";
				}else{
					viewStr+='<div>'
						+'<input type="hidden" name="'+dataModelDef.code+"."+field.code+'">'
						+'<span id="'+uploadPickId+'"><span class="glyphicon glyphicon-open"></span>上传文件</button>'
						+'</span>'
						+'<div id="'+listViewId+'" class="fileList">'
						+'</div>'
						+'<div id="'+listUploadViewId+'">'
						+'</div>';
				}
				
				
				viewStr+='</div>';
			return viewStr;
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.setValue=function(value){
			if(value){
				attSetId=value;
				require(['wiseMen/module/FileLoader'],function(FileLoader){
	        		new FileLoader({
	        			listView:$("#"+listViewId),
	        			attSetId:value,
	        			readOnly:param.readOnly
	        		});
	        	});
			}
		};
		this.afterAppend=function(){
			if(!param.readOnly==true){
				webUtils.fileUploader({
					uploadPick:"#"+uploadPickId,
					uploadList:"#"+listUploadViewId,
					isSingle:field.isSingleSelect,
					afterInit:function(fileUploader,proxy){
						uploader=fileUploader;
					},
					onUploadSuccess:function(file,response,attSetId_){
						attSetId=attSetId_;
					}
		        });
			}
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			return attSetId||"";
		};
	};
	Control.richText=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var ctrId=webUtils.uuid();
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
			currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>';
				if(param.readOnly){
					viewStr+="<pre class='pre-scrollable dataBox'>";
					viewStr+="</pre>";
				}else{
					viewStr+='<textarea id="'+ctrId+'" name="'+dataModelDef.code+"."+field.code+'">'
					+'</textarea>'
				}
				
				viewStr+='</div>';
			return viewStr;
		}
		this.afterAppend=function(){
			if(!param.readOnly==true){
				me.ue = UE.getEditor(ctrId);
			}
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			return me.ue.getContent();
		};
		this.setValue=function(v){
			if(param.readOnly){
				currentCtr.find(".dataBox").html(v);
			}else{
				me.ue.ready(function() {
					if(v){
						me.ue.setContent(v);
					}
				});
			}
			
		};
	};
	Control.dropdownList=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		var keyMap={};
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">';
			viewStr+='<label>'+field.name+'</label>';
			if(param.readOnly){
				viewStr+="<pre class='dataBox'>";
				viewStr+="</pre>";
				if(field.options){
					var e=eval("("+field.options+")");
					$.each(e,function(i,n){
						keyMap[n.value]=n.title;
					});
				}else if(field.dataDicCode){
					var dataDicItems=webUtils.getDataDicItems(field.dataDicCode);
					$.each(dataDicItems,function(i,n){
						keyMap[n.value]=n.name;
					});
				}
			}else{
				viewStr+='<select class="form-control" name="'+dataModelDef.code+"."+field.code+'" ';
				if(field.viewCheckType){
					viewStr+=' check-type="'+field.viewCheckType+'" ';
				}
				viewStr+='>';
				if(field.options){
					var e=eval("("+field.options+")");
					$.each(e,function(i,n){
						keyMap[n.value]=n.title;
						viewStr+='<option value='+n.value;
						if(n.isDefault=='true'){
							viewStr+=' selected="selected" ';
						}
						viewStr+='>'+n.title;
						viewStr+='</option>';
					});
				}else if(field.dataDicCode){
					var dataDicItems=webUtils.getDataDicItems(field.dataDicCode,false);
					$.each(dataDicItems,function(i,n){
						keyMap[n.value]=n.name;
						viewStr+='<option value='+n.value;
						viewStr+='>'+n.name;
						viewStr+='</option>';
					});
				}
				viewStr+='</select>';
			}
			
			viewStr+='</div>';
			return viewStr;
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.setValue=function(v){
			if(param.readOnly){
				currentCtr.find(".dataBox").html(keyMap[v]);
			}else{
				currentCtr.find('select[name="'+dataModelDef.code+"."+field.code+'"]').val(v);
			}
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.afterAppend=function(){
			
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			return currentCtr.find("select").val();
		};
	};
	Control.chkbox=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		var keyMap={};
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">';
			viewStr+='<label>'+field.name+'</label>';
			if(param.readOnly){
				viewStr+="<pre class='dataBox'>";
				viewStr+="</pre>";
				if(field.options){
					var e=eval("("+field.options+")");
					$.each(e,function(i,n){
						keyMap[n.value]=n.title;
					});
				}else if(field.dataDicCode){
					var dataDicItems=webUtils.getDataDicItems(field.dataDicCode);
					$.each(dataDicItems,function(i,n){
						keyMap[n.value]=n.name;
					});
				}
			}else{
				viewStr+='<div>';
				if(field.options){
					var e=eval("("+field.options+")");
					$.each(e,function(i,n){
						keyMap[n.value]=n.title;
						
						viewStr+='<label class="checkbox-inline">';
						viewStr+='<input type="checkbox" name="'+dataModelDef.code+"."+field.code+'" value='+n.value;
						if(n.isDefault=='true'){
							viewStr+=' checked="checked" ';
						}
						if(field.viewCheckType){
							viewStr+=' check-type="'+field.viewCheckType+'" ';
						}
						viewStr+='>'+n.title+'</input>';
						viewStr+='</label>';
					});
				}else if(field.dataDicCode){
					var dataDicItems=webUtils.getDataDicItems(field.dataDicCode);
					$.each(dataDicItems,function(i,n){
						keyMap[n.value]=n.name;
						viewStr+='<label class="checkbox-inline">';
						viewStr+='<input type="checkbox" name="'+dataModelDef.code+"."+field.code+'" value='+n.value;
						if(field.viewCheckType){
							viewStr+=' check-type="'+field.viewCheckType+'" ';
						}
						viewStr+='>'+n.name+'</input>';
						viewStr+='</label>';
					});
				}
				viewStr+='</div>';
			}
			viewStr+='</div>';
			return viewStr;
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.afterAppend=function(){
			
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			var v=[];
			currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]:checked').each(function(){
				v.push($(this).val());
			});
			return v.join(",");
		};
		this.setValue=function(v){
			if(v || v==0){
				var ids=v.split?v.split(","):[v];
				if(param.readOnly){
					var names=[];
					$.each(ids,function(i,n){
						names.push(keyMap[n]);
					});
					currentCtr.find(".dataBox").html(names.join(","));
				}else{
					$.each(ids,function(i,n){
						currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"][value=' + n + ']').attr("checked", "checked");   
					})
				}
				
			}
		};
	};
	Control.radio=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		var keyMap={};
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">';
			viewStr+='<label>'+field.name+'</label>';
			
			if(param.readOnly){
				viewStr+="<pre class='dataBox'>";
				if(field.defaultValue){
					viewStr+=field.defaultValue;
				}
				viewStr+="</pre>";
				
				if(field.options){
					var e=eval("("+field.options+")");
					$.each(e,function(i,n){
						keyMap[n.value]=n.title;
					});
				}else if(field.dataDicCode){
					var dataDicItems=webUtils.getDataDicItems(field.dataDicCode);
					$.each(dataDicItems,function(i,n){
						keyMap[n.value]=n.name;
					});
				}
			}else{
				viewStr+='<div>';
				if(field.options){
					var e=eval("("+field.options+")");
					$.each(e,function(i,n){
						keyMap[n.value]=n.title;
						
						viewStr+='<label class="radio-inline">';
						viewStr+='<input type="radio" name="'+dataModelDef.code+"."+field.code+'" value='+n.value;
						if(n.isDefault=='true'){
							viewStr+=' checked="checked" ';
						}
						if(field.viewCheckType){
							viewStr+=' check-type="'+field.viewCheckType+'" ';
						}
						viewStr+='>'+n.title+'</input>';
						viewStr+='</label>';
					});
				}else if(field.dataDicCode){
					var dataDicItems=webUtils.getDataDicItems(field.dataDicCode);
					$.each(dataDicItems,function(i,n){
						keyMap[n.value]=n.name;
						
						viewStr+='<label class="radio-inline">';
						viewStr+='<input type="radio" name="'+dataModelDef.code+"."+field.code+'" value='+n.value;
						if(field.viewCheckType){
							viewStr+=' check-type="'+field.viewCheckType+'" ';
						}
						viewStr+='>'+n.name+'</input>';
						viewStr+='</label>';
					});
				}
				viewStr+='</div>';
			}
			
			viewStr+='</div>';
			return viewStr;
		}
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.afterAppend=function(){
			
		};
		this.getTableName=function(){
			return table.tableName;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			var v=currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]:checked').val();
			return v||"";
		};
		this.setValue=function(v){
			if(v || v==0){
				var ids=v.split?v.split(","):[v];
				if(param.readOnly){
					var names=[];
					$.each(ids,function(i,n){
						names.push(keyMap[n]);
					});
					currentCtr.find(".dataBox").html(names.join(","));
				}else{
					$.each(ids,function(i,n){
						currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"][value=' + n + ']').attr("checked","checked");   
					});
				}
				
			}
		};
	};
	Control.date=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>'
				if(param.readOnly){
					viewStr+="<pre class='dataBox'>";
					viewStr+="</pre>";
				}else{
					viewStr+='<input type="text" name="'+dataModelDef.code+"."+field.code+'" class="form-control" '
					if(field.viewCheckType){
						viewStr+=' check-type="'+field.viewCheckType+'" ';
					}
					viewStr+='>';
				}
				
				viewStr+='</div>';
			return viewStr;
		}
		this.iniEvent=function(){
			currentCtr.find("input").datetimepicker({
				format: 'YYYY-MM-DD',
				locale:"zh-cn"
			});
		};
		this.afterAppend=function(){
			if(!param.readOnly==true){
				me.iniEvent();
			}
		};
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			return currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]').val();
		};
		this.setValue=function(v){
			if(v){
				if(param.readOnly){
					currentCtr.find(".dataBox").html(webUtils.parseTimestamp(v,"yyyy-MM-dd"));
				}else{
					currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]')
					.val(webUtils.parseTimestamp(v,"yyyy-MM-dd"));
				}
			}
		};
		
	};
	Control.dateTime=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>'
				if(param.readOnly){
					viewStr+="<pre class='dataBox'>";
					viewStr+="</pre>";
				}else{
					viewStr+='<input type="text" name="'+dataModelDef.code+"."+field.code+'" class="form-control" '
					if(field.viewCheckType){
						viewStr+=' check-type="'+field.viewCheckType+'" ';
					}
					viewStr+='>';
				}
				
				viewStr+='</div>';
			return viewStr;
		}
		this.iniEvent=function(){
			currentCtr.find("input").datetimepicker({
				format: 'YYYY-MM-DD HH:mm',
				locale:"zh-cn"
			});
		};
		this.afterAppend=function(){
			if(!param.readOnly){
				me.iniEvent();
			}
		};
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			return currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]').val();
		};
		this.setValue=function(v){
			if(v){
				if(param.readOnly){
					currentCtr.find(".dataBox").html(webUtils.parseTimestamp(v,"yyyy-MM-dd HH:mm"));
				}else{
					currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]')
					.val(webUtils.parseTimestamp(v,"yyyy-MM-dd HH:mm"));
				}
				
			}
		};
		
	};
	
	Control.hidden=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		this.isHiddenField=true;

		this.isReadOnly=function(){
			return false;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
		}
		this.createDomHtml=function(){
			var viewStr='<input type="hidden" name="'+dataModelDef.code+"."+field.code+'">';
			return viewStr;
		}
		this.iniEvent=function(){
		};
		this.afterAppend=function(){
			me.iniEvent();
		};
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getValue=function(){
			return currentCtr.val();
		};
		this.setValue=function(v){
			if(v){
				currentCtr.val(v);
			}
		};
	};
	
	Control.grid=function(param){
		alert("未提供该组件的独立支持");
		var dataModelDef=param.dataModelDef;
		var queryDef;
		var me=this;
		var gridViewDom;
		var gridToolbarDom;
		var gridDom;
		var gridScriptDom;
		var gridDef;
		this.createDom=function(){
			gridViewDom=$("<div>");
			
			gridToolbarDom=$('<div class="form-inline" role="form"></div>');
			gridViewDom.append(gridToolbarDom);
			
			gridDom=$('<table>');
			gridViewDom.append(gridDom);
			
			gridScriptDom=$('<div style="display: none;"></div>');
			gridViewDom.append(gridScriptDom);
			
			return gridViewDom;
		}
		this.iniEvent=function(){
		};
		this.afterAppend=function(){
			gridDef=new QueryDefTpl({
				code:dataModelDef.code,//查询定义的别名
				scriptDiv:gridScriptDom,//引用脚本的div
				toolBarDiv:gridToolbarDom,//工具栏定义栏
				gridDiv:gridDom,
				side:param.side
			});
		};
		this.getViewObj=function(){
			return me.createDom();
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return "fields";
		};
		this.getValue=function(){
			return null;
		};
		this.getGridDef=function(){
			return gridDef;
		};
		this.getGrid=function(){
			return gridDef.getGrid();
		}
	};
	
	Control.popUpSearchList=function(param){
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		var me=this;
		var currentCtr;
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			var viewStr=me.createDomHtml();
				currentCtr=$(viewStr);
			return currentCtr;
		}
		this.createDomHtml=function(){
			var viewStr='<div class="form-group">'
				+'<label>'+field.name+'</label>';
				if(param.readOnly){
					viewStr+="<pre class='dataBox'>";
					if(field.defaultValue){
						viewStr+=field.defaultValue;
					}
					viewStr+="</pre>";
				}else{
					viewStr+='<input type="text" name="'+dataModelDef.code+"."+field.code+'" class="form-control" '
					if(field.viewCheckType){
						viewStr+=' check-type="'+field.viewCheckType+'" ';
					}
					viewStr+='>';
				}
				viewStr+='</div>';
			return viewStr;
		}
		this.iniEvent=function(){
			currentCtr.find("input").click(function(){
				var $thisInputCtr=$(this);
				var hasSelectedDatas=currentCtr.data("data");
				var code=field.relQueryDefCode||field.relDataModelCode;
				var selectModel=field.type=="singleRef"?"single":"multi";
				
				if(field.type=="singleRef"){
					hasSelectedDatas=hasSelectedDatas?[hasSelectedDatas]:[];
				}
				
				requirejs(['wiseMen/dataModel/QuerySelector'],function(QuerySelector){
					var querySelector=new QuerySelector({
						code:code,
						selectModel:selectModel,
						hasSelected:hasSelectedDatas,
						viewFieldCode:field.relDataModelViewFieldCode.toUpperCase(),
						onSelected:function(datas){
							var titles=[];
							
							if(field.type=="singleRef"){
								var data=datas[0];
								var id=data.id;
								var title=data.title;
								currentCtr.data("data",{id:id,title:title});
								currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]').val(title);
							}else{
								var ctrDatas=[];
								$.each(datas,function(i,data){
									var id=data.id;
									var title=data.title;
									titles.push(title);
									ctrDatas.push({id:id,title:title});
								})
								currentCtr.data("data",ctrDatas);
								currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]').val(titles.join(","));
							}
						}
					})
				});
			});
		};
		this.afterAppend=function(){
			me.iniEvent();
		};
		this.getViewObj=function(){
			me.createDom();
			return currentCtr;
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getFieldName=function(){
			return field.name;
		};
		this.getValue=function(){
			return currentCtr.data("data");
		};
		this.setValue=function(v){
			if(v){
				var titles=[];
				if($.isArray(v)){
					$.each(v,function(i,n){
						var t=n.TITLE?n.TITLE:n.title;
						titles.push(t);
					});
				}else{
					titles.push(v.title);
				}
				
				if(param.readOnly){
					currentCtr.find(".dataBox").html(titles.join(","));
				}else{
					currentCtr.find('input[name="'+dataModelDef.code+"."+field.code+'"]').val(titles.join(","));
					currentCtr.data("data",v);
				}
				
			}
		};
		
	};
	
	Control.subModel=function(param){
		var me=this;
		var dataModelDef=param.dataModelDef;
		var field=param.field;
		
		var queryDef;
		var gridViewDom;
		var gridToolbarDom;
		var gridDom;
		var gridScriptDom;
		var gridDef;
		
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		this.createDom=function(){
			gridViewDom=$("<div>"
					+'<div class="box-header with-border">'
					
					+'<h3 class="box-title">'
					+"</h3>"
					
					+'<div class="box-tools pull-right">'
					+"</div>"
					
					+"</div>"
					
					+'<div class="box-body">'
					+"</div>"
					
					+'<div class="box-script" div style="display: none;">'
					+"</div>"
					
					+"</div>");
			
			
			gridToolbarDom=$('<div class="form-inline" role="form"></div>');
			gridViewDom.find(".box-tools").append(gridToolbarDom);
			
			gridDom=$('<table class="table table-bordered table-hover" style="width:100%">');
			gridViewDom.find(".box-body").append(gridDom);
			
			gridScriptDom=gridViewDom.find(".box-script");
			//gridViewDom.append(gridScriptDom);
			
			return gridViewDom;
		}
		this.iniEvent=function(){
		};
		this.afterAppend=function(){
			gridDef=new QueryDefTpl({
				code:field.relQueryDefCode||field.relDataModelCode,//查询定义的别名
				scriptDiv:gridScriptDom,//引用脚本的div
				toolBarDiv:gridToolbarDom,//工具栏定义栏
				gridDiv:gridDom,
				model:"clientEdit"
			});
		};
		this.getViewObj=function(){
			return me.createDom();
		};
		this.getRequiredSapce=function(){
			return field.ctlRequiredSpace;
		};
		this.getDataModelCode=function(){
			return dataModelDef.code;
		};
		this.getFieldCode=function(){
			return field.code;
		};
		this.getFieldName=function(){
			return field.name;
		};
		this.getValue=function(){
			var datas=gridDef.getData(false);
			var r=[];
			$.each(datas,function(i,n){
				if(!n.ID){
					r.push(n);
				}
			});
			return r;
		};
		this.setValue =function(v,datas){
			var dataId=datas["id"];
			if(dataId){
				var ajaxData={};
				ajaxData[field.relDataModelFieldCode]=dataId;
				me.getGridDef().loadOnServer(ajaxData);
			}
		};
		this.getGridDef=function(){
			return gridDef;
		};
		this.getGrid=function(){
			return gridDef.getGrid();
		}
		this.isReadOnly=function(){
			return param.readOnly==true;
		}
		
	};
	
	//每个ctr定义几个方法
	Control.spaceClass={s:'s',m:'m',l:'l',d:'d'};
	Control.CtrMap={
		singleText:Control.singleText,
		multiText:Control.multiText,
		dictionary:Control.dictionary,
		userSelector:Control.userSelector,
		orgSelector:Control.orgSelector,
		adminDivSelector:Control.adminDivSelector,
		fileUpload:Control.fileUpload,
		richText:Control.richText,
		dropdownList:Control.dropdownList,
		chkbox:Control.chkbox,
		radio:Control.radio,
		date:Control.date,
		dateTime:Control.dateTime,
		hidden:Control.hidden,
		grid:Control.grid,
		popUpSearchList:Control.popUpSearchList,
		subModel:Control.subModel
	};
	Control.factory=function(param){
		var me=this;
		var ctrClass=Control.CtrMap[param.field.controlType];
		if(ctrClass){
			return new ctrClass(param);
		}else{
			return null;
		}
	};
	
	return Control;
});
	